package com.centit.framework.ip.businessdata.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.centit.framework.common.ResponseMapData;
import com.centit.framework.components.CodeRepositoryUtil;
import com.centit.framework.core.controller.BaseController;
import com.centit.framework.core.dao.CodeBook;
import com.centit.framework.ip.businessdata.po.MessageInfo;
import com.centit.framework.ip.businessdata.service.MessageInfoManager;
import com.centit.framework.ip.businessdata.service.TodoItemManager;
import com.centit.framework.ip.util.layuiResultUtil;
import com.centit.support.algorithm.StringBaseOpt;
import com.centit.support.database.utils.PageDesc;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Map;


@Controller
@RequestMapping("/messageInfo")
public class MessageInfoController extends BaseController {
	private static final Log log = LogFactory.getLog(MessageInfoController.class);
	
	@Resource
	private MessageInfoManager messageInfoMag;

    /**
     * 查询所有   支撑平台消息数据表【T_MESSAGE_INFO】  列表
     *
     * @param request  {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @return {data:[]}
     */
    @RequestMapping(value = "/listMessageInfo",method = RequestMethod.GET)
    public void listMessageInfo(String[] field, HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("X-Frame-Options", "SAMEORIGIN");

        try {
            request.getRequestDispatcher("/modules/dashboard/messageinfo/messageinfo.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(method = RequestMethod.GET)
    public void list(String[] field, HttpServletRequest request, HttpServletResponse response) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm"); // 设置时间格式
        //解析参数信息
        Map<String, Object> searchColumn = convertSearchColumn(request);
        //解析分页信息
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        Integer pageNo = 1;
        Integer pageSize = 20;
        pageNo = StringUtils.isNotEmpty(page) ? Integer.valueOf(page) : pageNo;
        pageSize = StringUtils.isNotEmpty(limit) ? Integer.valueOf(limit) : pageSize;
        PageDesc pageDesc = new PageDesc(pageNo, pageSize);
        String dataType=(String) searchColumn.get("dataType");
        if("1".equals(dataType)){
        searchColumn.put("userCode",getLoginUserCode(request));
        //默认未读
           String ifread=(String) searchColumn.get("ifread");
           if(StringUtils.isBlank(ifread)){
               searchColumn.put("ifread","1");
           }
        }else{
            searchColumn.put("ifread",null);
        }
        searchColumn.put("isValid","T");
        searchColumn.put(CodeBook.SELF_ORDER_BY, "createTime desc");


        JSONArray jsonArrayObjects = messageInfoMag.listObjectsAsJson(searchColumn, pageDesc);

        if (null == jsonArrayObjects){
            jsonArrayObjects = new JSONArray();
        }

        //翻译字典项
        if (jsonArrayObjects.size() > 0) {
            for (int i = 0; i < jsonArrayObjects.size(); i++) {
                JSONObject jsonObject1 = jsonArrayObjects.getJSONObject(i);
                //翻译字典项
                if (!StringBaseOpt.isNvl(jsonObject1.getString("systemId"))) {
                    jsonObject1.put("systemId", getDicValue(jsonObject1.getString("systemId"), "ptbmxx"));
                }
            }

        }
        ResponseMapData resData = new ResponseMapData();
        resData.addResponseData(OBJLIST, jsonArrayObjects);
        resData.addResponseData(PAGE_DESC, pageDesc);
        layuiResultUtil.layuiwriteResponseDataAsJson(resData, pageDesc, response);


    }
    @RequestMapping(value = "/loadMessageInfoOne/{recId}",method = RequestMethod.GET)
    public void loadMessageInfoOne(@PathVariable String recId, HttpServletRequest request, HttpServletResponse response) {
        MessageInfo messageInfo= messageInfoMag.getObjectById(recId);
        request.setAttribute("messageInfo",messageInfo);
        try {
            if("1".equals(messageInfo.getDataType())){
                messageInfo.setIfread("0");//消息提醒标记为已读
                messageInfoMag.mergeObject(messageInfo);
            }
            request.getRequestDispatcher("/modules/dashboard/messageinfo/messageinfoView.jsp").forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 翻译字典项
    private String getDicValue(String code, String type) {
        return CodeRepositoryUtil.getValue(type,code);
    }


    }
